package com.dingreading.cloud.web.service.impl;

import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.web.entity.TellHistoryCode;
import com.dingreading.cloud.web.entity.table.TellHistoryCodeTableDef;
import com.dingreading.cloud.web.mapper.TellHistoryCodeMapper;
import com.dingreading.cloud.web.service.TellHistoryCodeService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  服务层实现。
 *
 * @author Administrator
 * @since 2024-04-07
 */
@Service
public class TellHistoryCodeServiceImpl extends BaseServiceImpl<TellHistoryCodeMapper, TellHistoryCode, TellHistoryCodeTableDef> implements TellHistoryCodeService {

    @Override
    protected TellHistoryCodeTableDef getTable() {
        return TellHistoryCodeTableDef.tellHistoryCode;
    }

    @Override
    public TellHistoryCode getByCode(String code) {
        return getMapper().selectOneByCondition(table.code.eq(code));
    }

    @Override
    public long getByOpenidAndCode(String openid) {
        return getMapper().selectCountByCondition(table.openid.eq(openid));
    }

    @Override
    public TellHistoryCode getByOpenid(String openid) {
        return getMapper().selectOneByCondition(table.openid.eq(openid));
    }

    @Override
    public Page<TellHistoryCode> pageList(PageUtil pageUtil, String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword)) {
            condition.and(table.code.like(keyword));
        }
        if (enabled != null) {
            if (enabled.equals(1))
                condition.and(table.openid.isNotNull());
            else
                condition.and(table.openid.isNull());
        }


        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public List<String> getByCodes(List<String> codes) {
        QueryWrapper wrapper = QueryWrapper.create().select(table.code)
                .from(table)
                .where(table.code.in(codes));
        return getMapper().selectObjectListByQueryAs(wrapper, String.class);
    }

}
